// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: linchuan

#pragma once

#include <cstdio>
#include <map>
#include <memory>
#include <set>
#include <string>

#include <rapidjson/document.h>

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/kraken/symbology/currency.h"
#include "coin2/exchange/kraken/symbology/product.h"

namespace coin2::exchange::kraken::order_v1 {

class KrakenParser : public base::order::BaseParser {
  using OrderContext = base::order::IOrderContext;

 public:
  const ParseResult& ParseRawMessage(const RawMessage& msg) override;
  AccountBalance ParseBalance(const JsonValue& doc) override;
  bool ParseRawBalance(const JsonValue& doc, RawBalanceMap* raw_balance_map);

 private:
  void ParseSubmitResponse();
  void ParseCancelResponse();
  void ParseRestOrdersInfo();
  void ParseRestFillInfoList();
  void ParseRestAccountBalance();
  void ParseWsMessage();
  void ParseListenKey();
  ProductOrderBundle ParseWsOrderInfoListV2(const JsonValue& data);
  ProductFillBundle ParseWsFillInfoListV2(const JsonValue& data);
  coin::proto::ProductOrderElement ParseRestOrderInfo(
      const std::string order_id,
      const JsonValue& data);
  const OrderContext* FindOrderContext(const std::string& order_id, const std::string& user_ref);
  void PublishRateCountUpdate();

 private:
  std::map<std::string, const OrderContext*> oc_map_;
  std::map<std::string, coin::proto::ProductOrderElement> ws_order_map_;
  bool is_account_balance_initialized_ = false;
  std::map<std::string, int> ratecount_map_;
};

}  // namespace coin2::exchange::kraken::order_v1
